package Day2025_03_24;

public class LeetCode279 {
    public int numSquares(int n) {
        int m = (int)Math.sqrt(n), INF = 0x3f3f3f3f;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= n; i++) dp[0][i] = INF;
        for (int i = 1; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= i * i) dp[i][j] = Math.min(dp[i][j], dp[i][j - i * i] + 1);
            }
        }
        return dp[m][n];
    }
}
